﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Castle.DynamicProxy;
using System.ComponentModel;
using NUnit.Framework;
using EasyProp;

namespace EasyProp.Tests
{
    [TestFixture]
    public class NotifyPropertyChangedFixture
    {
        [Test]
        public void fires_property_changed()
        {
            EasyPropBuilder easyPropBuilder = new EasyPropBuilder();
            SimpleBusinessObject businessObject = easyPropBuilder.Build<SimpleBusinessObject>();

            bool gotEvent = false;
            businessObject.PropertyChanged += delegate(object sender, PropertyChangedEventArgs e)
            {
                if (e.PropertyName == "OrderStatus")
                {
                    gotEvent = true;
                }
            };

            businessObject.OrderStatus = "Dispatched to customer";
            Assert.IsTrue(gotEvent);
        }

        [Test]
        public void sender_is_of_our_built_object_type()
        {
            EasyPropBuilder easyPropBuilder = new EasyPropBuilder();
            SimpleBusinessObject businessObject = easyPropBuilder.Build<SimpleBusinessObject>();

            object theSender = null;
            businessObject.PropertyChanged += delegate(object sender, PropertyChangedEventArgs e)
            {
                theSender = sender;
            };

            businessObject.OrderStatus = "Dispatched to customer";
            Assert.IsTrue(theSender is SimpleBusinessObject);
        }

        [AfterPropertySetFilter(typeof(CountInvocationsFilter))]
        [AfterPropertySetFilter(typeof(CountInvocationsFilter))]
        public class ClassWithTwoAttributes
        {
            public virtual string MyProperty { get; set; }
        }

        [AfterPropertySetFilter(typeof(DoNothingFilter))]
        [AfterPropertySetFilter(typeof(DoNothingFilter))]
        public class PropertyCallCountingObject
        {
            public virtual string MyProperty
            {
                get { return ""; }
                set
                {
                    CallCount++;
                }
            }

            public int CallCount = 0;
        }

        [AfterPropertySetFilter(typeof(NotifyPropertyChanged))]
        public class SimpleBusinessObject : INotifyPropertyChanged
        {
            public virtual string OrderStatus { get; set; }

            #region INotifyPropertyChanged Members

            public event PropertyChangedEventHandler PropertyChanged;

            void GetRidOfCompilerWarnings()
            {
                this.PropertyChanged(null, null);
            }

            #endregion
        }

        [AfterPropertySetFilter(typeof(NotifyPropertyChanged))]
        public class ClassWithSetterBody : INotifyPropertyChanged
        {
            public virtual string Status
            {
                get { return _status; }
                set
                {
                    _status = value;
                }
            }

            #region INotifyPropertyChanged Members

            public event PropertyChangedEventHandler PropertyChanged;

            void GetRidOfCompilerWarnings()
            {
                this.PropertyChanged(null, null);
            }

            #endregion

            public string _status;
        }

    }
}
